import {
  AsyncStorage
}from 'react-native';
export const readData = async (key) => {
  try {
    let result = await AsyncStorage.getItem(key)
      return result
  } catch(error) {
    //Alert.alert("读取失败：" + error)
  }
};
//生成日期选择数据
export const createDateData=()=>{
  let date = [];
  var currDate = new Date()
  var year = currDate.getFullYear()
  var month = currDate.getMonth()+1
  for(let i=1970;i<=year;i++){
      let month = [];
      for(let j = 1;j<13;j++){
          let day = [];
          if(j === 2){
              for(let k=1;k<29;k++){
                  day.push(k+'日');
              }
              //Leap day for years that are divisible by 4, such as 2000, 2004
              if(i%4 === 0){
                  day.push(29+'日');
              }
          }
          else if(j in {1:1, 3:1, 5:1, 7:1, 8:1, 10:1, 12:1}){
              for(let k=1;k<32;k++){
                  day.push(k+'日');
              }
          }
          else{
              for(let k=1;k<31;k++){
                  day.push(k+'日');
              }
          }
          let _month = {};
          _month[j+'月'] = day;
          month.push(_month);
      }
      let _date = {};
      _date[i+'年'] = month;
      date.push(_date);
  }
  return date;
}
export const getCurrentDate=()=>{
  var currDate = new Date()
  var year = currDate.getFullYear()
  var month = (currDate.getMonth()+1).toString()
  month = month.padStart(2,'0')
  var dateDay = currDate.getDate().toString()
  dateDay = dateDay.padStart(2,'0')
  let time = year+'-'+month+'-'+dateDay
  return time;
}
export const addByTransDate=(dateParameter, num)=>{
    let translateDate = "",
      dateString = "",
      monthString = "",
      dayString = "";
    translateDate = dateParameter
      .replace("年", "/")
      .replace("月", "/")
      .replace("日", "/");
    let newDate = new Date(translateDate);
    newDate = newDate.valueOf();
    newDate = newDate + num * 24 * 60 * 60 * 1000;
    newDate = new Date(newDate);
    //如果月份长度少于2，则前加 0 补位
    if ((newDate.getMonth() + 1).toString().length == 1) {
      monthString = 0 + "" + (newDate.getMonth() + 1).toString();
    } else {
      monthString = (newDate.getMonth() + 1).toString();
    }
    //如果天数长度少于2，则前加 0 补位
    if (newDate.getDate().toString().length == 1) {
      dayString = 0 + "" + newDate.getDate().toString();
    } else {
      dayString = newDate.getDate().toString();
    }
    dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString;
    return dateString;
  }
export const reduceByTransDate=(dateParameter, num)=>{
    let translateDate = "",
      dateString = "",
      monthString = "",
      dayString = "";
    translateDate = dateParameter
      .replace("年", "/")
      .replace("月", "/")
      .replace("日", "/");
    let newDate = new Date(translateDate);
    newDate = newDate.valueOf();
    newDate = newDate - num * 24 * 60 * 60 * 1000;
    newDate = new Date(newDate);
    //如果月份长度少于2，则前加 0 补位
    if ((newDate.getMonth() + 1).toString().length == 1) {
      monthString = 0 + "" + (newDate.getMonth() + 1).toString();
    } else {
      monthString = (newDate.getMonth() + 1).toString();
    }
    //如果天数长度少于2，则前加 0 补位
    if (newDate.getDate().toString().length == 1) {
      dayString = 0 + "" + newDate.getDate().toString();
    } else {
      dayString = newDate.getDate().toString();
    }
    dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString;
    return dateString;
  }